package com.rong.DAO;

import com.opensymphony.xwork2.ActionContext;
import com.rong.hibernate.BaseHibernateDAO;
import com.rong.hibernate.Const;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.rong.ORM.ZynPrice1;
import com.rong.ORM.ZynPrice34;
import com.rong.ORM.ZynQueryPrice;
import com.rong.ORM.ZynUser;

/**
 * A data access object (DAO) providing persistence and search support for
 * ZynPrice34 entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.rong.DAO.ZynPrice34
 * @author MyEclipse Persistence Tools
 */

public class ZynPrice34DAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ZynPrice34DAO.class);
	// property constants
	public static final String CAR_ID = "carId";
	public static final String COM_ID_A = "comIdA";
	public static final String COM_ID_B = "comIdB";
	public static final String PRICE_TRAFC = "priceTrafc";
	public static final String STMT_STYLE_TRAFC = "stmtStyleTrafc";
	public static final String UNIT_TRAFC = "unitTrafc";
	public static final String PRICE_SELL = "priceSell";
	public static final String STMT_STYLE_SELL = "stmtStyleSell";
	public static final String UNIT_SELL = "unitSell";
	public static final String REMARK = "remark";
	public static final String EXT_ATTR1 = "extAttr1";
	public static final String EXT_ATTR2 = "extAttr2";
	public static final String EXT_ATTR3 = "extAttr3";
	public static final String EXT_ATTR4 = "extAttr4";
	public static final String EXT_ATTR5 = "extAttr5";
	public static final String EXT_ATTR6 = "extAttr6";
	public static final String EXT_ATTR7 = "extAttr7";
	public static final String EXT_ATTR8 = "extAttr8";

	public void save(ZynPrice34 transientInstance) {
		log.debug("saving ZynPrice34 instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().save(transientInstance);
			ts.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(ZynPrice34 persistentInstance) {
		log.debug("deleting ZynPrice34 instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			getSession().delete(persistentInstance);
			ts.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public ZynPrice34 findById(java.lang.Integer id) {
		log.debug("getting ZynPrice34 instance with id: " + id);
		try {
			ZynPrice34 instance = (ZynPrice34) getSession().get(ZynPrice34.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ZynPrice34 instance) {
		log.debug("finding ZynPrice34 instance by example");
		try {
			List results = getSession().createCriteria(
					"com.rong.DAO.ZynPrice34").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ZynPrice34 instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ZynPrice34 as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCarId(Object carId) {
		return findByProperty(CAR_ID, carId);
	}

	public List findByComIdA(Object comIdA) {
		return findByProperty(COM_ID_A, comIdA);
	}

	public List findByComIdB(Object comIdB) {
		return findByProperty(COM_ID_B, comIdB);
	}

	public List findByPriceTrafc(Object priceTrafc) {
		return findByProperty(PRICE_TRAFC, priceTrafc);
	}

	public List findByStmtStyleTrafc(Object stmtStyleTrafc) {
		return findByProperty(STMT_STYLE_TRAFC, stmtStyleTrafc);
	}

	public List findByUnitTrafc(Object unitTrafc) {
		return findByProperty(UNIT_TRAFC, unitTrafc);
	}

	public List findByPriceSell(Object priceSell) {
		return findByProperty(PRICE_SELL, priceSell);
	}

	public List findByStmtStyleSell(Object stmtStyleSell) {
		return findByProperty(STMT_STYLE_SELL, stmtStyleSell);
	}

	public List findByUnitSell(Object unitSell) {
		return findByProperty(UNIT_SELL, unitSell);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByExtAttr1(Object extAttr1) {
		return findByProperty(EXT_ATTR1, extAttr1);
	}

	public List findByExtAttr2(Object extAttr2) {
		return findByProperty(EXT_ATTR2, extAttr2);
	}

	public List findByExtAttr3(Object extAttr3) {
		return findByProperty(EXT_ATTR3, extAttr3);
	}

	public List findByExtAttr4(Object extAttr4) {
		return findByProperty(EXT_ATTR4, extAttr4);
	}

	public List findByExtAttr5(Object extAttr5) {
		return findByProperty(EXT_ATTR5, extAttr5);
	}

	public List findByExtAttr6(Object extAttr6) {
		return findByProperty(EXT_ATTR6, extAttr6);
	}

	public List findByExtAttr7(Object extAttr7) {
		return findByProperty(EXT_ATTR7, extAttr7);
	}

	public List findByExtAttr8(Object extAttr8) {
		return findByProperty(EXT_ATTR8, extAttr8);
	}

	public List findAll() {
		log.debug("finding all ZynPrice34 instances");
		try {
			String queryString = "from ZynPrice34";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ZynPrice34 merge(ZynPrice34 detachedInstance) {
		log.debug("merging ZynPrice34 instance");
		try {
			 Transaction ts = null;
             ts = getSession().beginTransaction();
			ZynPrice34 result = (ZynPrice34) getSession().merge(
					detachedInstance);
			ts.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ZynPrice34 instance) {
		log.debug("attaching dirty ZynPrice34 instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ZynPrice34 instance) {
		log.debug("attaching clean ZynPrice34 instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	

	/**
	 * 查找并返回符合条件的价格记录
	 */

	public List<ZynPrice34> findPriceQueryByPage(int page, int rowsPerPage,ZynQueryPrice queryCrt) {
		
		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
	
		Transaction ts2=null;
		ts2=getSession().beginTransaction();
		getSession().clear();
				
		Criteria crt = getSession().createCriteria(ZynPrice34.class);
		  Criteria crtCom = crt.createCriteria("comObj");
		  Criteria crtComC = crt.createCriteria("comObjC");
		    Criteria crtCar = crt.createCriteria("carObj");		     
		crt.addOrder(org.hibernate.criterion.Order.desc("id"));
		
		if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()!=null){
			crt.add(Restrictions.and(Restrictions.ge("invalidationDate", queryCrt.getStartDate()), Restrictions.le("invalidationDate", queryCrt.getEndDate())));
		}else if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()==null){
			crt.add(Restrictions.ge("invalidationDate", queryCrt.getStartDate()));	
		}else if(queryCrt.getStartDate()==null&&queryCrt.getEndDate()!=null){
			crt.add(Restrictions.le("invalidationDate", queryCrt.getEndDate()));	
		}
		
		if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //采热地点
			
			crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
		}
		if(queryCrt.getToComName()!=null&&!queryCrt.getToComName().equalsIgnoreCase("")){ 		//客户
			
			crtComC.add(Restrictions.like("comName", queryCrt.getToComName(),MatchMode.ANYWHERE));
		}
		
		if(queryCrt.getCarNum()!=null&&!queryCrt.getCarNum().equalsIgnoreCase("")){  			//车辆
			
			crtCar.add(Restrictions.like("num", queryCrt.getCarNum(),MatchMode.ANYWHERE));
		}
		
		
		if(queryCrt.getTransfType()!=null&&!queryCrt.getTransfType().equalsIgnoreCase("")){ 	//运费结算方式
			crt.add(Restrictions.eq("stmtStyleTrafc", queryCrt.getTransfType()));	
		}
				
		if(queryCrt.getTrsfprice()!=null&&queryCrt.getTrsfprice()>0){ 			//运费单价
			crt.add(Restrictions.ge("priceTrafc", queryCrt.getTrsfprice()));	
		}
			
		if(queryCrt.getGiveheatType()!=null&&!queryCrt.getGiveheatType().equalsIgnoreCase("")){ 	//供热类型
			crt.add(Restrictions.eq("extAttr1", queryCrt.getGiveheatType()));	
		}
		
		if(queryCrt.getSellType()!=null&&!queryCrt.getSellType().equalsIgnoreCase("")){ 	//供热结算方式
			crt.add(Restrictions.eq("stmtStyleSell", queryCrt.getSellType()));	
		}
				
		if(queryCrt.getSellprice()!=null&&queryCrt.getSellprice()>0){ 			//供热单价
			crt.add(Restrictions.ge("priceSell", queryCrt.getSellprice()));	
		}		
		
		if(queryCrt.getPlusprice()!=null&&queryCrt.getPlusprice()>0){ 			//奖罚价格
			crt.add(Restrictions.ge("extAttr2", String.valueOf(queryCrt.getPlusprice())));	
		}
		
		//--权限判断,分公司只能看本公司单据，本部可以查看全部		
		if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
			
		}else{			
					//所属分公司
			crtCom.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
			  //匹配单据的所属公司
		}
		
		
		crt.setMaxResults(rowsPerPage); // 每页最多显示几条
		crt.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
		ts2.commit();
		List<ZynPrice34> list = crt.list();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("==========comlist by query======"+list.get(i).getComNameA());
			
		}
        
		

		return list;
	}
	/**
	 * 查找并返回所有价格记录数
	 */

	public List<ZynPrice34> priceTatolRows(ZynQueryPrice queryCrt) {
		getSession().clear();

		ActionContext actionContext = ActionContext.getContext();         
	     Map session = actionContext.getSession(); 
	     ZynUser loginUser=(ZynUser)session.get("loginUser");   //登录用户	
			
			Criteria crt = getSession().createCriteria(ZynPrice34.class);
			  Criteria crtCom = crt.createCriteria("comObj");
			  Criteria crtComC = crt.createCriteria("comObjC");
			    Criteria crtCar = crt.createCriteria("carObj");		     
			crt.addOrder(org.hibernate.criterion.Order.desc("id"));
			
			if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()!=null){
				crt.add(Restrictions.and(Restrictions.ge("invalidationDate", queryCrt.getStartDate()), Restrictions.le("invalidationDate", queryCrt.getEndDate())));
			}else if(queryCrt.getStartDate()!=null&&queryCrt.getEndDate()==null){
				crt.add(Restrictions.ge("invalidationDate", queryCrt.getStartDate()));	
			}else if(queryCrt.getStartDate()==null&&queryCrt.getEndDate()!=null){
				crt.add(Restrictions.le("invalidationDate", queryCrt.getEndDate()));	
			}
			
			if(queryCrt.getFromComName()!=null&&!queryCrt.getFromComName().equalsIgnoreCase("")){ //采热地点
				
				crtCom.add(Restrictions.like("comName", queryCrt.getFromComName(),MatchMode.ANYWHERE));
			}
			if(queryCrt.getToComName()!=null&&!queryCrt.getToComName().equalsIgnoreCase("")){ 		//客户
				
				crtComC.add(Restrictions.like("comName", queryCrt.getToComName(),MatchMode.ANYWHERE));
			}
			
			if(queryCrt.getCarNum()!=null&&!queryCrt.getCarNum().equalsIgnoreCase("")){  			//车辆
				
				crtCar.add(Restrictions.like("num", queryCrt.getCarNum(),MatchMode.ANYWHERE));
			}
			
			
			if(queryCrt.getTransfType()!=null&&!queryCrt.getTransfType().equalsIgnoreCase("")){ 	//运费结算方式
				crt.add(Restrictions.eq("stmtStyleTrafc", queryCrt.getTransfType()));	
			}
					
			if(queryCrt.getTrsfprice()!=null&&queryCrt.getTrsfprice()>0){ 			//运费单价
				crt.add(Restrictions.ge("priceTrafc", queryCrt.getTrsfprice()));	
			}
				
			if(queryCrt.getGiveheatType()!=null&&!queryCrt.getGiveheatType().equalsIgnoreCase("")){ 	//供热类型
				crt.add(Restrictions.eq("extAttr1", queryCrt.getGiveheatType()));	
			}
			
			if(queryCrt.getSellType()!=null&&!queryCrt.getSellType().equalsIgnoreCase("")){ 	//供热结算方式
				crt.add(Restrictions.eq("stmtStyleSell", queryCrt.getSellType()));	
			}
					
			if(queryCrt.getSellprice()!=null&&queryCrt.getSellprice()>0){ 			//供热单价
				crt.add(Restrictions.ge("priceSell", queryCrt.getSellprice()));	
			}		
			
			if(queryCrt.getPlusprice()!=null&&queryCrt.getPlusprice()>0){ 			//奖罚价格
				crt.add(Restrictions.ge("extAttr2", String.valueOf(queryCrt.getPlusprice())));	
			}
			
			//--权限判断,分公司只能看本公司单据，本部可以查看全部		
			if(loginUser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){			
				
			}else{			
						//所属分公司
				crtCom.add(Restrictions.eq("extAttr3", String.valueOf(loginUser.getComId())));
				  //匹配单据的所属公司
			}
			
		List<ZynPrice34> list = crt.list();
		return list;
	}
}